/// -----------------------------------------------------------------------------------------------------------
/// Module      :  SalesForceWCFAdapterConnection.cs
/// Description :  Defines the connection to the target system.
/// -----------------------------------------------------------------------------------------------------------

#region Using Directives
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.ServiceModel.Channels.Common;
using global::WCFLOBTalk.SalesForceWCF.Helpers;

#endregion

namespace WCFLOBTalk.SalesForceWCF
{
    public class SalesForceWCFAdapterConnection : IConnection
    {
        #region Private Fields

        private SalesForceWCFAdapterConnectionFactory connectionFactory;
        private string connectionId;

        // serviceInstance
        private SFService.SforceService connectionService = null;

        #endregion Private Fields

        /// <summary>
        /// Initializes a new instance of the SalesForceWCFAdapterConnection class with the SalesForceWCFAdapterConnectionFactory
        /// </summary>
        public SalesForceWCFAdapterConnection(SalesForceWCFAdapterConnectionFactory connectionFactory)
        {
            //MessageBox.Show("Connection object created!");

            this.connectionFactory = connectionFactory;
            this.connectionId = Guid.NewGuid().ToString();
        }

        #region Public Properties

        /// <summary>
        /// Gets the ConnectionFactory
        /// </summary>
        public SalesForceWCFAdapterConnectionFactory ConnectionFactory
        {
            get
            {
                return this.connectionFactory;
            }
        }

        public SFService.SforceService ServiceInstance
        {
            get
            {
                return this.connectionService;
            }

        }

        #endregion Public Properties

        #region IConnection Members

        /// <summary>
        /// Closes the connection to the target system
        /// </summary>
        public void Close(TimeSpan timeout)
        {
            //
            //TODO: Implement physical closing of the connection
            //
        }

        /// <summary>
        /// Returns a value indicating whether the connection is still valid
        /// </summary>
        public bool IsValid(TimeSpan timeout)
        {
            //
            //TODO: Implement physical checking for the validity of the opened connection
            //
            return SFHelper.IsStillValidConnection();
            
        }

        /// <summary>
        /// Opens the connection to the target system.
        /// </summary>
        public void Open(TimeSpan timeout)
        {
           
            SFService.SforceService c = new WCFLOBTalk.SalesForceWCF.SFService.SforceService();

            // when connected to internet:
            SalesForceWCFAdapterConnectionUri sfUri = this.connectionFactory.ConnectionProperties;
            this.connectionService =  SFHelper.Login(sfUri.Uri.ToString(), this.connectionFactory.Credentials.UserName.UserName, this.connectionFactory.Credentials.UserName.Password, sfUri.SecurityToken, false, sfUri.version);

            //otherwise, read from file

        }

        /// <summary>
        /// Clears the context of the Connection. This method is called when the connection is set back to the connection pool
        /// </summary>
        public void ClearContext()
        {
            //
            //TODO: Implement clear context to set the connection back to the pool.
            //
            throw new NotImplementedException("The method or operation is not implemented.");
        }

        /// <summary>
        /// Builds a new instance of the specified IConnectionHandler type
        /// </summary>
        public TConnectionHandler BuildHandler<TConnectionHandler>(MetadataLookup metadataLookup)
             where TConnectionHandler : class, IConnectionHandler
        {

            if (typeof(IAsyncOutboundHandler).IsAssignableFrom(typeof(TConnectionHandler)))
            {
                return new SalesForceWCFAdapterAsyncOutboundHandler(this, metadataLookup) as TConnectionHandler;
            }
            if (typeof(IOutboundHandler).IsAssignableFrom(typeof(TConnectionHandler)))
            {
                return new SalesForceWCFAdapterOutboundHandler(this, metadataLookup) as TConnectionHandler;
            }
            if (typeof(IAsyncInboundHandler).IsAssignableFrom(typeof(TConnectionHandler)))
            {
                return new SalesForceWCFAdapterAsyncInboundHandler(this, metadataLookup) as TConnectionHandler;
            }
            if (typeof(IInboundHandler).IsAssignableFrom(typeof(TConnectionHandler)))
            {
                return new SalesForceWCFAdapterInboundHandler(this, metadataLookup) as TConnectionHandler;
            }
            if (typeof(IMetadataResolverHandler).IsAssignableFrom(typeof(TConnectionHandler)))
            {
                return new SalesForceWCFAdapterMetadataResolverHandler(this, metadataLookup) as TConnectionHandler;
            }
            if (typeof(IMetadataBrowseHandler).IsAssignableFrom(typeof(TConnectionHandler)))
            {
                return new SalesForceWCFAdapterMetadataBrowseHandler(this, metadataLookup) as TConnectionHandler;
            }
            if (typeof(IMetadataSearchHandler).IsAssignableFrom(typeof(TConnectionHandler)))
            {
                return new SalesForceWCFAdapterMetadataSearchHandler(this, metadataLookup) as TConnectionHandler;
            }

            return default(TConnectionHandler);
        }

        /// <summary>
        /// Aborts the connection to the target system
        /// </summary>
        public void Abort()
        {
            //
            //TODO: Implement abort logic. DO NOT throw an exception from this method
            //
            throw new NotImplementedException("The method or operation is not implemented.");
        }


        /// <summary>
        /// Gets the Id of the Connection
        /// </summary>
        public String ConnectionId
        {
            get
            {
                return connectionId;
            }
        }

        #endregion IConnection Members
    }
}
